package org.infodavid.common.model.impl;

import org.infodavid.common.model.impl.AbstractBasicDataObjectImpl;
import org.infodavid.common.model.security.ISecuredObjectIdentifier;

/**
 * The Class SecuredObjectIdentifierImpl.
 */
public class SecuredObjectIdentifierImpl extends AbstractBasicDataObjectImpl implements
    ISecuredObjectIdentifier {

	/** serialVersionUID. */
	private static final long serialVersionUID = 1549072855151143125L;

	/** The type. */
	private String type;

	/** The identifier. */
	private String identifier;

	/**
	 * The Constructor.
	 * @throws Exception the exception
	 */
	protected SecuredObjectIdentifierImpl() throws Exception {
		super();
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.security.ISecuredObjectIdentifier#getType()
	 */
	public String getType() {
		return type;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.security.ISecuredObjectIdentifier#getIdentifier()
	 */
	public String getIdentifier() {
		return identifier;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.impl.model.DataObjectImpl#init()
	 */
	@Override
	public void init() throws Exception {
		super.init();

		type = null;
		identifier = null;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.security.ISecuredObjectIdentifier#setType(java.lang.String)
	 */
	public void setType(final String value) {
		if (!equals(type, value)) {
			type = value;
			markModified(TYPE);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * org.infodavid.common.model.security.ISecuredObjectIdentifier#setIdentifier(java.io.Serializable
	 * )
	 */
	public void setIdentifier(final String value) {
		if (!equals(identifier, value)) {
			identifier = value;
			markModified(IDENTIFIER);
		}
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.impl.model.DataObjectImpl#toString(java.lang.StringBuffer)
	 */
	@Override
	protected StringBuffer toString(final StringBuffer builder) {
		super.toString(builder);

		builder.append(" [type=");
		builder.append(type);
		builder.append(" ,identifier=");
		builder.append(identifier);
		builder.append("]");

		return builder;
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;

		result = prime * result + (getType() == null ? 0 : getType().hashCode());
		result = prime * result + (getIdentifier() == null ? 0 : getIdentifier().hashCode());

		return result;
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(final Object obj) {
		if (this == obj) {
			return true;
		}

		if (obj == null) {
			return false;
		}

		if (!(obj instanceof ISecuredObjectIdentifier)) {
			return false;
		}

		final ISecuredObjectIdentifier other = (ISecuredObjectIdentifier)obj;

		if (getIdentifier() == null) {
			if (other.getIdentifier() != null) {
				return false;
			}
		}
		else if (!getIdentifier().equals(other.getIdentifier())) {
			return false;
		}

		if (getType() == null) {
			if (other.getType() != null) {
				return false;
			}
		}
		else if (!getType().equals(other.getType())) {
			return false;
		}

		return true;
	}
}
